/**
 * RenweiWu
 * RegionalStatisticsService.java
 * 2017年8月30日
 * RegionalStatisticsService
 * TODO
 * 
 */
package com.etrans.web.gps.business.report.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.report.domain.TouristVehicleAreaInout;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.common.Constant;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.DateUtil;
import com.mongodb.QueryOperators;

/**
 * @Description:TODO
 * @author RenweiWu
 * @date 2017年8月30日
 * @ClassName:  RegionalStatisticsService  
 * @param:   
 */
@Service
public class RegionalStatisticsService extends BaseService{
	@Autowired
	private CommonService commonService;
	private static SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd"); 
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map<String,Object> queryEnterpirseCar(int area_id,String enterpirseName,String startTime,String endTime,String vehicle_no){
		Map<String,Object> map =new HashMap<String, Object>();
		HashMap<String,Object> whereMap = new HashMap<String,Object>();
		List<String> namelist = null;
		namelist = new ArrayList<String>();
		List<HashMap<String,Object>> lists =commonService.queryEnterAndPlatByRoleNotLimit(area_id,enterpirseName);
		for (HashMap<String, Object> hashMap : lists){
			if(enterpirseName==null||"".equals(enterpirseName)){
				namelist.add((String)hashMap.get("entName"));
			}else{
				String name =(String)hashMap.get("entName");
				if(enterpirseName.equals(name)){
					namelist.add(enterpirseName);
				}
			}
		}
		if(vehicle_no!=null&&!"".equals(vehicle_no)){
			String vehicle = vehicle_no.split("_")[0];
			String color = vehicle_no.split("_")[1];
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("selected", area_id);
			param.put("vehicle_no",vehicle);
			param.put("vehicle_color",color);	
			List<HashMap<String,Object>> list =commonService.queryVeListByRole(param);
			if(list!=null){
				for (HashMap<String, Object> hashMap : list) {
					if(hashMap.get("vehicle_no").equals(vehicle)){
						whereMap.put("vehicle_no",vehicle);
						whereMap.put("vehicle_color",Integer.parseInt(color));
					}
				}
			}
		}else{
			HashMap<String,Object> nameMap = new HashMap<String,Object>();
			nameMap.put(QueryOperators.IN,namelist);
			whereMap.put("enterprise_name",nameMap);
		}
		HashMap<String,Object> timeMap = new HashMap<String,Object>();
		if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime)){
			if(startTime.equals(endTime)){
				timeMap.put(QueryOperators.GTE,DateUtil.getISODate(startTime+" 00:00:00"));
				timeMap.put(QueryOperators.LTE,DateUtil.getISODate(endTime+" 23:59:59"));
			}else{
				if(startTime!=null&&!"".equals(startTime)){
					timeMap.put(QueryOperators.GTE,DateUtil.getStringToDate(startTime));
				}
				if (endTime!=null&&!"".equals(endTime)) {
					timeMap.put(QueryOperators.LTE,DateUtil.getStringToDate(endTime));
				}
			}
		}else {
			if (endTime!=null&&!"".equals(endTime)) {
				try {
					timeMap.put(QueryOperators.GTE,DF.parse((DateUtil.getDateToString(new Date(),null))));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		whereMap.put("analyse_date",timeMap);
		List<TouristVehicleAreaInout> list = MGTemplete.find("egov","tourist_vehicle_area_inout", whereMap,TouristVehicleAreaInout.class,true);
		List  barXAxisData =new ArrayList();
		List  barSeriesData =new ArrayList();
		List  vehicleIdList = new ArrayList();
		Map<String,Object>  vmap = new HashMap<String,Object>();
		Map<String,Object> imap= new HashMap<String,Object>();
		Map<String,Integer> rsmap = new HashMap<String,Integer>();
		TouristVehicleAreaInout s = new TouristVehicleAreaInout();
		for (int i=0;i<list.size();i++) {
			s=list.get(i);
			imap.put(s.getEnterprise_name(),s.getEnterprise_name());
			if(rsmap.get(s.getEnterprise_name())!=null&&rsmap.get(s.getEnterprise_name())!=0){
				rsmap.put(s.getEnterprise_name(),rsmap.get(s.getEnterprise_name())+1);
			}else {
				rsmap.put(s.getEnterprise_name(),1);
			}
			vmap.put(s.getEnterprise_name(),s.getEnterprise_name());
		}
		for (Entry<String, Integer> entry :rsmap.entrySet()) {
			barXAxisData.add(entry.getKey());
			barSeriesData.add(entry.getValue());
			vehicleIdList.add(vmap.get(entry.getKey()));
		}

		map.put("series", barSeriesData);
		map.put("xAxisData", barXAxisData);
		map.put("vehicleIdList",vehicleIdList);
		return  map;
	}
	
	
	public  Map<String,Object>  getpieChart(String enterprise_name,String startTime,String endTime,String vehicleId){
		Map<String,Object> map = new HashMap<String,Object>();
		Map< String,Integer> map1 = new HashMap<String,Integer>();
		HashMap<String,Object> whereMap = new HashMap<String,Object>();
		if(enterprise_name!=null&&!"".equals(enterprise_name)){
			whereMap.put("enterprise_name",enterprise_name);
		}
		if(vehicleId!=null&&!"".equals(vehicleId)){
			String vehicle = vehicleId.split("_")[0];
			String color = vehicleId.split("_")[1];
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("selected", vehicleId);
			param.put("vehicle_no",vehicle);
			param.put("vehicle_color",color);	
			List<HashMap<String,Object>> list =commonService.queryVeListByRole(param);
			if(list!=null){
				for (HashMap<String, Object> hashMap : list) {
					if(hashMap.get("vehicle_no").equals(vehicle)){
						whereMap.put("vehicle_no",vehicle);
						whereMap.put("vehicle_color",Integer.parseInt(color));
					}
				}
			}
		}
		HashMap<String,Object> timeMap = new HashMap<String,Object>();
		if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime)){
			if(startTime.equals(endTime)){
				timeMap.put(QueryOperators.GTE,DateUtil.getISODate(startTime+" 00:00:00"));
				timeMap.put(QueryOperators.LTE,DateUtil.getISODate(endTime+" 23:59:59"));
			}else{
				if(startTime!=null&&!"".equals(startTime)){
					timeMap.put(QueryOperators.GTE,DateUtil.getStringToDate(startTime));
				}
				if (endTime!=null&&!"".equals(endTime)) {
					timeMap.put(QueryOperators.LTE,DateUtil.getStringToDate(endTime));
				}
			}
		}else {
			if (endTime!=null&&!"".equals(endTime)) {
				try {
					timeMap.put(QueryOperators.GTE,DF.parse((DateUtil.getDateToString(new Date(),null))));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		whereMap.put("analyse_date",timeMap);
		List<TouristVehicleAreaInout> list = MGTemplete.find("egov","tourist_vehicle_area_inout", whereMap,TouristVehicleAreaInout.class,true);
		Set<Long> areaIdlist = new HashSet<Long>();
		for (TouristVehicleAreaInout rs : list) {
			areaIdlist.add(rs.getArea_id());
		}
		Map<String,Object> areaMap = new HashMap<String,Object>();
		if(list != null && list.size() > 0){
			areaMap.put("areaIdlist",areaIdlist);
			for (TouristVehicleAreaInout rs : list) {
				if(map1.get(rs.getArea_name())!=null){
					map1.put(rs.getArea_name(),(int)map1.get(rs.getArea_name())+1);
				}else{
					map1.put(rs.getArea_name(),1);
				}
			}
		}
		map.put("list",list);
		map.put("map", map1);
		return map;
		
	}
	
	public  PageVo getTables(Map<String,Object> map){
		String enterprise_name =(String)map.get("enterprise_name");
		String vehicleId =(String)map.get("vehicleId");
		String startTime= (String)map.get("startTime");
		String endTime =(String)map.get("endTime");
		int page = (int)map.get("page");
		int pageSize = (int)map.get("pageSize");
		PageVo vo = new PageVo();
		HashMap<String,Object> whereMap = new HashMap<String,Object>();
		HashMap<String,Object> sortMap = new HashMap<String,Object>();
		if(enterprise_name!=null&&!"".equals(enterprise_name)){
			whereMap.put("enterprise_name",enterprise_name);;
		}
		if(vehicleId!=null&&!"".equals(vehicleId)){
			String vehicle = vehicleId.split("_")[0];
			String color = vehicleId.split("_")[1];
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("selected", vehicleId);
			param.put("vehicle_no",vehicle);
			param.put("vehicle_color",color);	
			List<HashMap<String,Object>> list =commonService.queryVeListByRole(param);
			if(list!=null){
				for (HashMap<String, Object> hashMap : list) {
					if(hashMap.get("vehicle_no").equals(vehicle)){
						whereMap.put("vehicle_no",vehicle);
						whereMap.put("vehicle_color",Integer.parseInt(color));
					}
				}
			}
		}
		HashMap<String,Object> timeMap = new HashMap<String,Object>();
		if(startTime!=null&&!"".equals(startTime)&&endTime!=null&&!"".equals(endTime)){
			if(startTime.equals(endTime)){
				timeMap.put(QueryOperators.GTE,DateUtil.getISODate(startTime+" 00:00:00"));
				timeMap.put(QueryOperators.LTE,DateUtil.getISODate(endTime+" 23:59:59"));
			}else{
				if(startTime!=null&&!"".equals(startTime)){
					timeMap.put(QueryOperators.GTE,DateUtil.getStringToDate(startTime));
				}
				if (endTime!=null&&!"".equals(endTime)) {
					timeMap.put(QueryOperators.LTE,DateUtil.getStringToDate(endTime));
				}
			}
		}else {
			if (endTime!=null&&!"".equals(endTime)) {
				try {
					timeMap.put(QueryOperators.GTE,DF.parse((DateUtil.getDateToString(new Date(),null))));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		whereMap.put("analyse_date",timeMap);
		sortMap.put("enter_time",Constant.MONGODB_DESC);	
		long countlist = MGTemplete.findCount("egov","tourist_vehicle_area_inout", whereMap);
		List<TouristVehicleAreaInout> list = MGTemplete.find("egov","tourist_vehicle_area_inout", whereMap,sortMap,page,pageSize,TouristVehicleAreaInout.class,true);
		vo.setTotal(new Long(countlist).intValue());
		vo.setRows(list);
		vo.setPage(page);
		return vo;
		
	}
	
	
	public List<Map<String,Object>> queryEnterpirse(int areaId){
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("FullID", String.format("%08d",areaId));
		return  this.selectList("regionalStatisticsQuery.queryTEnterpirse",param);
	}
}
